package com.liuyu.mall2.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuyu.mall2.entity.Goods;
import com.liuyu.mall2.mapper.GoodsMapper;
import com.liuyu.mall2.pojo.PageBean;
import com.liuyu.mall2.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 刘宇
 * @classname GoodServiceimpl
 * @time 2025/4/22
 * @project mall2
 */
@Service
public class GoodsServiceImpl implements GoodsService {

@Autowired
private GoodsMapper goodsMapper;
    @Override
    public void add(Goods goods) {
        if(goods.getHottest()==null){
            goods.setHottest("n");
        }
        if (goods.getNewest()==null){
            goods.setNewest("n");
        }
        goods.setSaleTime(LocalDateTime.now());
        goods.setStatus("y");
        goodsMapper.insert(goods);
    }
    @Override
    public List<Goods> findAll() {
        return goodsMapper.list();
    }

    @Override
    public PageBean<Goods> findByPage(int pageNo) {
        PageBean<Goods> pageBean = new PageBean<>();
        //根据参数进行分页:第一个参数就是页码，第二个表示分页大小
        //对紧跟在这句代码之后的查询就会有分页效果
        PageHelper.startPage(pageNo,2);
        List<Goods> list = goodsMapper.list();
        PageInfo<Goods> pageInfo = new PageInfo<>(list);

        pageBean.setData(pageInfo.getList());
        pageBean.setTotalPage(pageInfo.getPages());
        pageBean.setTotal((int)pageInfo.getTotal());
        pageBean.setPage(pageNo);
        pageBean.setPageSize(2);
        pageBean.setPageCount(pageInfo.getList().size());


        return pageBean;
    }
}


